package com.hzdesk.media.koreabatchmatvector.utils;

import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.bean.DcfBean;
import com.hzdesk.media.koreabatchmatvector.bean.PathBean;
import com.hzdesk.media.koreabatchmatvector.bean.UserBean;
import com.hzdesk.media.koreabatchmatvector.bean.UserListBean;
import com.hzdesk.media.koreabatchmatvector.bean.XmlMeetingBean;
import com.hzdesk.media.koreabatchmatvector.bean.XmlUserBean;
import com.hzdesk.media.koreabatchmatvector.bean.XmlVideoFile;
import com.thoughtworks.xstream.XStream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.List;

/**
 * XMLUtil
 *
 * @author Mr.Lee
 */
public class XMLUtil implements Serializable {
    private static final String TAG = "XMLUtil";
    private static String a;
    private static String b;
    /**
     *
     */
    private static final long serialVersionUID = 6154550871387518043L;

    /**
     * 生成批注文件
     * @param list    List (PathBean)
     * @param name    FileName
     * @param length  FileLength
     * @param
     */
    public static void CreatXMLFile(List<PathBean> list, List<XmlVideoFile> xmlVideoFileList, List<XmlUserBean> xmlUserBeanList,String name, long length) {
        DcfBean bean = new DcfBean();
        bean.setFilename(name);
        bean.setLength(length);
        bean.setFilesize("");
        bean.setFileList(xmlVideoFileList);
        bean.setList(list);
        bean.setUserList(xmlUserBeanList);
        XStream stream2 = new XStream();
        stream2 = new XStream();
        stream2.alias("pathbean", PathBean.class);
        stream2.alias("dcf", DcfBean.class);
        stream2.alias("file",XmlVideoFile.class);
        stream2.alias("user",XmlUserBean.class);
        stream2.processAnnotations(DcfBean.class);

        stream2.alias("dcf", DcfBean.class);

        String xml = stream2.toXML(bean);
        a = xml;
        //Log.i(TAG+ "creat:", xml);
        FileOutputStream fos;
        BufferedWriter writer;

        //FileUtil.write(AppConfig.DESKMEDIA_XMLFILE+"/"+name, xml);

        //FileUtil.createNewFile(new File(AppConfig.DESKMEDIA_XMLFILE+"/"+name));
        File file = new File(name);
        FileUtil.createNewFile(file);

        try {
            fos = new FileOutputStream(file, false);
            fos.write(xml.getBytes());
            //writer = new BufferedWriter(new OutputStreamWriter(fos));
            //writer.write(xml);
            //writer.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // todo 读取File的内容转成 DcfBean 读取批注文件
    public static DcfBean readXMLFile(File file) {

        DcfBean bean = new DcfBean();
        XStream stream2 = new XStream();
        stream2 = new XStream();
        stream2.alias("pathbean", PathBean.class);
        stream2.alias("dcf", DcfBean.class);
        stream2.processAnnotations(DcfBean.class);
        StringBuilder builder = new StringBuilder();
        try {
            InputStream instream = new FileInputStream(file);
            if (instream != null) {
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line = "";
                // 分行读取
                while ((line = buffreader.readLine()) != null) {
                    builder.append(line);
                }
                // Log.i("<><>", content);
                bean = (DcfBean) stream2.fromXML(builder.toString());
                instream.close();
                b = builder.toString();
                //Log.i(TAG+ "creat:", b);
            }
        } catch (FileNotFoundException e) {
            Log.d("TestFile", "The File doesn't not exist.");
        } catch (IOException e) {
            Log.d("TestFile", e.getMessage());
        }

        return bean;

    }

    public static boolean isSame() {
        return a == b;
    }

    /**
     * 创建用户配置文件
     *
     * @param list userbean
     * @param name filename
     */
    public static void CreatXMLFile(List<UserBean> list, String name) {
        UserListBean bean = new UserListBean();
        bean.setList(list);
        XStream stream = new XStream();
        String xml = stream.toXML(bean);
        FileOutputStream fileOutputStream;
        File file = new File(name);
        FileUtil.createNewFile(file);
        try {
            fileOutputStream = new FileOutputStream(file, false);
            fileOutputStream.write(xml.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

   //获取 用户配置文件 todo 读取file的内容放到StringBuilder容器中在转换成UserListBean 获取里面的List<UserBean> 集合
    public static List<UserBean> ReadXmlFileByUser(File file) {
        UserListBean bean = new UserListBean();
        XStream stream = new XStream();
        StringBuilder builder = new StringBuilder();
        try {
            InputStream instream = new FileInputStream(file);
            if (instream != null) {
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line = "";
                // 分行读取
                while ((line = buffreader.readLine()) != null) {
                    builder.append(line);
                }
                bean = (UserListBean) stream.fromXML(builder.toString());
                instream.close();
            }
        } catch (Exception e) {}
        return bean.getList();
    }

    /**
     * 创建会议信息文件
     */
    public static void CreatMeetingXml(XmlMeetingBean bean,String path){
        XStream stream = new XStream();
        stream.alias("meeting", XmlMeetingBean.class);
        String xml = stream.toXML(bean);
        FileOutputStream fileOutputStream;
        File file = new File(path);
        FileUtil.createNewFile(file);
        try {
            fileOutputStream = new FileOutputStream(file, false);
            //fileOutputStream.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>".getBytes());
            fileOutputStream.write(xml.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


}
